//
//  PWSHelper.m
//  PWSCalendar
//
//  Created by Sylar on 3/14/14.
//  Copyright (c) 2014 PWS. All rights reserved.
//

#import "PWSHelper.h"

@implementation PWSHelper

//获取下一个月的时间,就是又滑
+ (NSDate*) GetNextMonth:(NSDate*)_date {
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:_date];
    
    NSInteger year  = comps.year;
    NSInteger month = comps.month;
    NSInteger day   = comps.day;
    
    // NSLog(@"%d === %d === %d",year,month,day);
    
    NSDate* rt = nil;
    
    if (day <= 28 || month == 12) {
        
        comps.month = month+1;
        rt = [cal dateFromComponents:comps];
        
    } else {
        
        NSString* ss = [NSString stringWithFormat:@"%ld-%ld-3", (long)year, month+1];
        NSDateFormatter* ff = [[NSDateFormatter alloc] init];
        [ff setDateFormat:@"yyyy-MM-dd"];
        
        NSDate* the_month = [ff dateFromString:ss];
        NSRange rng = [cal rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:the_month];
        NSInteger day_in_month = rng.length;
        
        NSString* datestring = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)year, month+1, MIN(day, day_in_month)];
        rt = [ff dateFromString:datestring];
        
        //NSLog(@" ss = %@  the_month = %@ day_in_month = %ld datestring = %@ rt = %@",ss,the_month,(long)day_in_month,datestring,rt);
    }
    
    
    
    return rt;
}
//获取上个月的时间,就是左滑
+ (NSDate*) GetPreviousMonth:(NSDate*)_date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:_date];
    
    NSInteger year  = comps.year;
    NSInteger month = comps.month;
    NSInteger day   = comps.day;
    
    // NSLog(@"%d === %d === %d 0000",year,month,day);
    
    NSDate* rt = nil;
    
    if (day <= 28 || month == 1) {
        
        comps.month = month-1;
        rt = [cal dateFromComponents:comps];
        
    } else {
        
        NSString* ss = [NSString stringWithFormat:@"%ld-%ld-3", (long)year, month-1];
        NSDateFormatter* ff = [[NSDateFormatter alloc] init];
        [ff setDateFormat:@"yyyy-MM-dd"];
        NSDate* the_month = [ff dateFromString:ss];
        NSRange rng = [cal rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:the_month];
        NSInteger day_in_month = rng.length;
        
        NSString* datestring = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)year, month-1, MIN(day, day_in_month)];
        rt = [ff dateFromString:datestring];
        
        //NSLog(@" ss = %@  the_month = %@ day_in_month = %ld datestring = %@ rt = %@",ss,the_month,(long)day_in_month,datestring,rt);
        
    }
    return rt;
}
//获取下周时间
+ (NSDate*) GetNextWeek:(NSDate*)_date
{
    NSDate* rt = [_date dateByAddingTimeInterval:7*3600*24];
    
    //    NSLog(@" 获取下周的时间 %@",[[HelpObject manager] getCurentTime:@"yyyy:MM:dd" dateN:rt]);
    
    return rt;
}
//获取上周时间
+ (NSDate*) GetPreviousWeek:(NSDate*)_date
{
    //时间戳
    NSDate* rt = [_date dateByAddingTimeInterval:-7*3600*24];
    
    //    NSLog(@" 获取下周的时间 %@",[[HelpObject manager] getCurentTime:@"yyyy-MM-dd" dateN:rt]);
    
    return rt;
}
//判断前后两个时间是不是一样的
+ (BOOL) CheckSameDay:(NSDate*)_date1 AnotherDate:(NSDate*)_date2
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString* s1 = [formatter stringFromDate:_date1];
    NSString* s2 = [formatter stringFromDate:_date2];
    BOOL rt = [s1 isEqualToString:s2];
    return rt;
}

//判断两个月份是不是一样的
+ (BOOL) CheckThisMonth:(NSDate*)_thisMonth NextMonth:(NSDate*)_nextMonth
{
    BOOL rt = [self CheckSameMonth:[self GetNextMonth:_thisMonth] AnotherMonth:_nextMonth];
    return rt;
}
//判断两个月份是不是一样的
+ (BOOL) CheckSameMonth:(NSDate*)_month1 AnotherMonth:(NSDate*)_month2
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents* m1 = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth fromDate:_month1];
    NSDateComponents* m2 = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth fromDate:_month2];
    BOOL rt = NO;
    if ((m1.year == m2.year) && (m1.month == m2.month))
    {
        rt = YES;
    }
    return rt;
}

//判断两个星期是不是一样的
+ (BOOL) CheckThisWeek:(NSDate*)_thisWeek NextWeek:(NSDate*)_nextWeek
{
    BOOL rt = [self CheckSameWeek:[self GetNextWeek:_thisWeek] AnotherWeek:_nextWeek];
    return rt;
}
//判断两个星期是不是一样的
+ (BOOL) CheckSameWeek:(NSDate*)_week1 AnotherWeek:(NSDate*)_week2
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents* w1 = [cal components:NSCalendarUnitYear|NSCalendarUnitWeekOfMonth fromDate:_week1];
    NSDateComponents* w2 = [cal components:NSCalendarUnitYear|NSCalendarUnitWeekOfMonth fromDate:_week2];
    BOOL rt = NO;
    if ((w1.weekday == w2.weekday) && ([_week1 timeIntervalSinceDate:_week2]<=24*3600*7))
    {
        rt = YES;
    }
    return rt;
}

@end
